package com.spring.controller;

import com.spring.common.entity.RemindTaskBean;
import com.spring.common.entity.Student;
import com.spring.common.task.DynamicScheduleTask;
import com.spring.common.validate.AddStuAndTeach;
import com.spring.common.validate.AddStudent;
import com.spring.common.validate.QueryDetail;
import com.spring.common.web.CommRes;
import com.spring.service.RemindTaskService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
public class CaseController {
    @Resource
    RemindTaskService remindTaskService;
    @Resource
    DynamicScheduleTask dynamicScheduleTask;

    @RequestMapping("/add")
    public void add4(@Valid @RequestBody RemindTaskBean remindTaskBean){
        remindTaskService.add(remindTaskBean);
    }

    @RequestMapping("/add2")
    public CommRes add2(@Valid @RequestBody RemindTaskBean remindTaskBean){
        if (remindTaskBean.getTaskName() == null) {
            CommRes.fail("调度任务名不能为空");
        }
        if (remindTaskBean.getTaskStatus() == null) {
            CommRes.fail("调度任务状态不能为空");
        }
        if (remindTaskBean.getTaskStatus()!=0||remindTaskBean.getTaskStatus()!=1) {
            CommRes.fail("调度任务状态错误,请更正");
        }
        if (remindTaskBean.getCron() != null) {
            String reg="\\d/\\d+ \\*{1} \\*{1} \\*{1} \\*{1} \\?{1}";
            if (!remindTaskBean.getCron().matches(reg)) {
                CommRes.fail("表达式格式错误,请更正");
            }
        }
        remindTaskService.add(remindTaskBean);
        return CommRes.success(remindTaskBean);
    }



    @RequestMapping("/add3")
    public CommRes add3(@Valid @RequestBody RemindTaskBean remindTaskBean){
        if (remindTaskBean.getTaskName() == null) {
            CommRes.fail("调度任务名不能为空");
        }
        remindTaskService.add(remindTaskBean);
        return CommRes.success(remindTaskBean);
    }

    @GetMapping("/list")
    public List<RemindTaskBean> list(){
        List<RemindTaskBean> remindTasks = dynamicScheduleTask.taskList();
        return remindTasks;
    }

    @PostMapping("/addStuaAndTeach")
    public String addStuaAndTeach(@Validated(AddStuAndTeach.class) @RequestBody Student student){
        System.out.println("学生的工号:"+student.getStuCode()+",学生的老师的姓名:"+student.getTeacher().getTecName());
        return "success";
    }

    @PostMapping("/add")
    public Student add(@Validated(AddStudent.class) @RequestBody Student student) {
        System.out.println(student.getStuName());
        return student;
    }

    @PostMapping("/detail")
    public String detail(@Validated(QueryDetail.class)@RequestBody Student student){
        System.out.println("学生id:"+student.getId());
        return "success";
    }
}
